package server.model.properties;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import model.dto.SQLConnectionDTO;

public class PropertiesLoader {

	public static SQLConnectionDTO loadDBMSProperties(String filePath) throws FileNotFoundException, IOException{
		SQLConnectionDTO response=new SQLConnectionDTO();
		File propFile=new File(filePath);
		Properties prop=new Properties();
		prop.load(new FileInputStream(propFile));

		response.setDriver(prop.getProperty("driver"));
		response.setUrl(prop.getProperty("url"));
		response.setUser(prop.getProperty("user"));
		response.setPassword(prop.getProperty("password"));

		return response;
	}

	public static void saveDBMSProperties(SQLConnectionDTO dto, String filePath) throws FileNotFoundException, IOException{
		File propFile=new File(filePath);
		Properties prop=new Properties();
		prop.setProperty("driver", dto.getDriver());
		prop.setProperty("url", dto.getUrl());
		prop.setProperty("user", dto.getUser());
		prop.setProperty("password", dto.getPassword());
		prop.store(new FileOutputStream(propFile), "Database Connection Config");
	}
}
